package com.haotunet.app.goldtreasure.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.bumptech.glide.Glide;
import com.goldplusgold.imageloader.lib.GlideRoundTransform;
import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.activity.DynamicDetailActivity;
import com.haotunet.app.goldtreasure.activity.MainActivity;
import com.haotunet.app.goldtreasure.adapter.RecommendGoldAdapter;
import com.haotunet.app.goldtreasure.cycleviewpager.lib.CycleViewPager;
import com.haotunet.app.goldtreasure.cycleviewpager.lib.CycleViewPager.ImageCycleViewListener;
import com.haotunet.app.goldtreasure.model.BannerInfo;
import com.haotunet.app.goldtreasure.model.BannerModel;
import com.haotunet.app.goldtreasure.model.BaseModel;
import com.haotunet.app.goldtreasure.model.GoldInfo;
import com.haotunet.app.goldtreasure.model.HomePageModel;
import com.haotunet.app.goldtreasure.model.ManageGold;
import com.haotunet.app.goldtreasure.model.MediaInfo;
import com.haotunet.app.goldtreasure.model.MsgInfo;
import com.haotunet.app.goldtreasure.model.NoticeInfo;
import com.haotunet.app.goldtreasure.model.RecommendProductBean;
import com.haotunet.app.goldtreasure.model.RecommendProductModel;
import com.haotunet.app.goldtreasure.net.ActionUtil;
import com.haotunet.app.goldtreasure.net.UrlUtil;
import com.haotunet.app.goldtreasure.other.Interface.OnPassBackListener;
import com.haotunet.app.goldtreasure.other.ViewFactory;
import com.haotunet.app.goldtreasure.utils.ActivityUtils;
import com.haotunet.app.goldtreasure.utils.NetworkUtil;
import com.haotunet.app.goldtreasure.utils.ShareStore;
import com.haotunet.app.goldtreasure.utils.Strs;
import com.haotunet.app.goldtreasure.utils.UIHelper;
import com.haotunet.app.goldtreasure.views.CampaignDialog;
import com.haotunet.app.goldtreasure.views.DefineListView;
import com.haotunet.app.goldtreasure.views.TitleView;
import com.haotunet.app.pulltorefresh.PullToRefreshLayout;
import com.haotunet.app.pulltorefresh.pullableView.PullableScrollView;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class HomePageFragment extends BaseFragment implements PullableScrollView.ScrollViewListener {

    @InjectView(R.id.invite_layout)
    LinearLayout inviteLayout;
    @InjectView(R.id.safety_guarantee_layout)
    LinearLayout safetyGuaranteeLayout;
    @InjectView(R.id.gold_price_txt)
    TextView goldPriceTxt;
    @InjectView(R.id.price_layout)
    LinearLayout priceLayout;
    @InjectView(R.id.txt_market_price)
    TextView txtMarketPrice;
    @InjectView(R.id.txt_gold_price)
    TextView txtGoldPrice;
    @InjectView(R.id.current_rate_txt)
    TextView currentRateTxt;
    @InjectView(R.id.buy_regualar_product_layout)
    LinearLayout buyRegualarProductLayout;
    @InjectView(R.id.goldTrendLayout)
    RelativeLayout goldTrendLayout;
    @InjectView(R.id.lv_recommend_gold)
    DefineListView lvRecommendGold;
    @InjectView(R.id.txt_tonne_decade)
    TextView txtTonneDecade;
    @InjectView(R.id.txt_tonne_unit)
    TextView txtTonneUnit;
    @InjectView(R.id.txt_kilogram_hundreds)
    TextView txtKilogramHundreds;
    @InjectView(R.id.txt_kilogram_decade)
    TextView txtKilogramDecade;
    @InjectView(R.id.txt_kilogram_unit)
    TextView txtKilogramUnit;
    @InjectView(R.id.txt_gram_hundreds)
    TextView txtGramHundreds;
    @InjectView(R.id.txt_gram_decade)
    TextView txtGramDecade;
    @InjectView(R.id.txt_gram_unit)
    TextView txtGramUnit;
    @InjectView(R.id.btn_policy)
    ImageView btnPolicy;
    @InjectView(R.id.root_layout)
    LinearLayout rootLayout;
    @InjectView(R.id.pull_scrollview)
    PullableScrollView pullScrollview;
    @InjectView(R.id.id_swipe_ly)
    PullToRefreshLayout mRefreshLayout;
    @InjectView(R.id.title_view_main)
    TitleView titleViewMain;
    @InjectView(R.id.iv_video_one)
    ImageView ivVideoOne;
    @InjectView(R.id.title_video_one)
    TextView titleVideoOne;
    @InjectView(R.id.iv_video_two)
    ImageView ivVideoTwo;
    @InjectView(R.id.title_video_two)
    TextView titleVideoTwo;
    private LinearLayout notice_parent_ll;
    private ViewFlipper notice_vf;
    private boolean isPrepared;
    private View view;
    private List<ImageView> imageList;
    private int mCurrPos;
    private HomePageModel model;
    Handler handler = new Handler();
    private CycleViewPager cycleViewPager;
    private boolean isActivityDialogShow;
    private boolean isScroll;
    private boolean isAddScrollTitleLayout;
    ArrayList<String> linkUrlArray = new ArrayList<>();
    ArrayList<String> titleList = new ArrayList<>();
    private final int GET_HOMEPAGE_INFO_SUCCESS = 2;
    private final int STATUS_GOLDPRICE_SUCCESS = 3;
    private final int STATUS_BANNER_SUCCESS = 4;
    private final int STATUS_RECOMMEND_PRODUCT_SUCCESS = 5;
    private FrameLayout main_notice;
    private TimerTask task;
    private Timer timer;
    private ShareStore mStore;
    private List<MediaInfo> mediaList;
    Runnable runnable;
    private Handler handle = new Handler() {

        public void handleMessage(Message msg) {
            switch (msg.what) {

                case GET_HOMEPAGE_INFO_SUCCESS:
                    rootLayout.setVisibility(View.VISIBLE);
                    model = (HomePageModel) msg.obj;
                    mStore.putString("start_img", model.getStartImg());
                    mStore.putString("start_img_url", model.getStartImgLinkUrl());
                    mStore.commit();
                    mediaList = model.getArticleList();
                    setMediaReportData(mediaList);
                    List<NoticeInfo> notificationList = model.getNotificationList();
                    if (!isAddScrollTitleLayout) {
                        if (notificationList != null && notificationList.size() > 0) {
                            initRollNotice();
                            isAddScrollTitleLayout = true;
                        }
                    }
                    if (titleList.size() == 0) {
                        for (int i = 0; i < notificationList.size(); i++) {
                            titleList.add(notificationList.get(i).getTitle());
                            linkUrlArray.add(notificationList.get(i).getContentUrl());
                        }
                    }
                    if (titleList.size() > 0) {
                        if (!isScroll) {
                            timedScrollingSubtitles();
                            isScroll = true;
                        }
                    }
                    String popupImage = model.getPopupImage();
                    String popupUrl = model.getPopupUrl();
                    if (getActivity() != null && !getActivity().isFinishing()) {
                        if (!TextUtils.isEmpty(popupImage)) {
                            showActivityDialog(getActivity(), popupImage, popupUrl);
                        }
                    }
                    break;
                case STATUS_GOLDPRICE_SUCCESS:
                    GoldInfo info = (GoldInfo) msg.obj;
                    txtMarketPrice.setText(info.getMarketGoldPrice() + "元/克");
                    currentRateTxt.setText(info.getCurrentGoldRate() + "%");
                    goldPriceTxt.setText(info.getCurrentGoldPrice());
                    if (getActivity() != null && !getActivity().isFinishing()) {
                        Animation loadAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.translate_anim);
                        goldPriceTxt.startAnimation(loadAnimation);
                    }
                    break;
                case STATUS_BANNER_SUCCESS:
                    BannerModel bannerModel = (BannerModel) msg.obj;
                    List<BannerInfo> bannerList = bannerModel.getBannerList();
                    if (bannerList.size() > 0) {
                        setBannerInfo(bannerList);
                    }
                    break;
                case STATUS_RECOMMEND_PRODUCT_SUCCESS:
                    RecommendProductModel productModel = (RecommendProductModel) msg.obj;
                    List<RecommendProductBean> items = productModel.getProductList();
                    setProductdata(items);
                    break;
                default:
                    break;
            }
        }

    };

    private void setMediaReportData(List<MediaInfo> mediaList) {
        if (getActivity() == null && getActivity().isFinishing()) {
            return;
        }
        if (mediaList != null && mediaList.size() >= 2) {
            titleVideoOne.setText(mediaList.get(0).getTitle());
            titleVideoTwo.setText(mediaList.get(1).getTitle());
            Glide.with(getActivity()).load(mediaList.get(0).getImageUrl()).placeholder(R.drawable.placeholder_video_home).error(R.drawable.placeholder_video_home).transform(new GlideRoundTransform(getActivity(), 4)).into(ivVideoOne);
            Glide.with(getActivity()).load(mediaList.get(1).getImageUrl()).placeholder(R.drawable.placeholder_video_home).error(R.drawable.placeholder_video_home).transform(new GlideRoundTransform(getActivity(), 4)).into(ivVideoTwo);
        }
    }

    public static HomePageFragment newInstance() {
        return new HomePageFragment();
    }

    public HomePageFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_home_page, container, false);
        ButterKnife.inject(this, view);
        mStore = new ShareStore(getActivity());
        initView();
        isPrepared = true;
        requestUnreadMsg();
        getGoldPrice();
        getManageGoldTotalQuantity();
        getBannerData();
        getRecommendProduct("");
        getHomepageInfo();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (titleList.size() > 0) {
            if (!isScroll) {
                timedScrollingSubtitles();
                isScroll = true;
            }
        }
    }

    private void initView() {
        titleViewMain.getSloganView().setVisibility(View.VISIBLE);
        titleViewMain.getGuideView().setVisibility(View.VISIBLE);
        titleViewMain.getNotificationView().setVisibility(View.VISIBLE);
        setRefreshListener();
        cycleViewPager = (CycleViewPager) getActivity().getFragmentManager()
                .findFragmentById(R.id.fragment_cycle_viewpager_content);
        lvRecommendGold.setFocusable(false);
        txtMarketPrice.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        initScrollListener();
    }

    private void initScrollListener() {
        ViewTreeObserver viewTreeObserver = cycleViewPager.getViewPager().getViewTreeObserver();
        viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                cycleViewPager.getViewPager().getViewTreeObserver().removeOnGlobalLayoutListener(this);
                pullScrollview.setScrollViewListener(HomePageFragment.this);
            }
        });

    }

    private void requestUnreadMsg() {
        if (mStore.getBoolean(Strs.IS_LOGIN, false)) {
            getUnReadMsg();
        }
    }

    /**
     * 请求是否有未读消息
     */
    private void getUnReadMsg() {
        ActionUtil actionUtil = new ActionUtil(getActivity());
        actionUtil.getUnReadMsg();
        actionUtil.setOnPassBackListener(new OnPassBackListener() {

            @Override
            public void passBack(BaseModel model) {
                if (!isActivityAvailable()) {
                    return;
                }
                MsgInfo msgInfo = (MsgInfo) model;
                if (msgInfo == null) {
                    return;
                }
                int unreadMsgCount = msgInfo.getUnreadMsgCount();
                if (unreadMsgCount != 0) {
                    titleViewMain.getNotificationView().setSelected(true);
                } else {
                    titleViewMain.getNotificationView().setSelected(false);
                }
            }
        });
    }

    private void getGoldPrice() {
        ActionUtil actionUtil = new ActionUtil(getActivity());
        actionUtil.getCurrentGoldPrice();
        actionUtil.setOnPassBackListener(new OnPassBackListener() {

            @Override
            public void passBack(BaseModel model) {
                if (!isActivityAvailable()) {
                    return;
                }
                GoldInfo goldInfo = (GoldInfo) model;
                if (goldInfo == null) {
                    return;
                }
                if (goldInfo.getErrorMsg() != null) {
                    UIHelper.showToast(getActivity(), goldInfo.getErrorMsg());
                } else {
                    Message msg = new Message();
                    msg.what = STATUS_GOLDPRICE_SUCCESS;
                    msg.obj = goldInfo;
                    handle.sendMessage(msg);
                    timerGetGoldPrice();
                }
            }
        });
    }

    /**
     * 定时器30秒刷新一次金价
     */
    private void timerGetGoldPrice() {
        runnable = new Runnable() {

            @Override
            public void run() {
                getGoldPrice();
            }
        };
        handle.postDelayed(runnable, 30000);
    }

    private void getManageGoldTotalQuantity() {
        ActionUtil actionUtil = new ActionUtil(getActivity());
        actionUtil.getManageGoldTotalQuantity();
        actionUtil.setOnPassBackListener(new OnPassBackListener() {

            @Override
            public void passBack(BaseModel model) {
                if (!isActivityAvailable()) {
                    return;
                }
                ManageGold manageGold = (ManageGold) model;
                if (manageGold == null) {
                    return;
                }
                if (manageGold.getErrorMsg() != null) {
                    UIHelper.showToast(getActivity(), manageGold.getErrorMsg());
                } else {
                    setManageGoldInfo(manageGold);
                }
            }
        });
    }

    private void setManageGoldInfo(ManageGold manageGold) {
        String tonne = manageGold.getTonne();
        String kilogram = manageGold.getKilogram();
        String gram = manageGold.getGram();
        if (!TextUtils.isEmpty(tonne) && tonne.length() == 2) {
            txtTonneDecade.setText(tonne.substring(0, 1));
            txtTonneUnit.setText(tonne.substring(1, 2));
        }
        if (!TextUtils.isEmpty(kilogram) && kilogram.length() == 3) {
            txtKilogramHundreds.setText(kilogram.substring(0, 1));
            txtKilogramDecade.setText(kilogram.substring(1, 2));
            txtKilogramUnit.setText(kilogram.substring(2, 3));
        }
        if (!TextUtils.isEmpty(gram) && gram.length() == 3) {
            txtGramHundreds.setText(gram.substring(0, 1));
            txtGramDecade.setText(gram.substring(1, 2));
            txtGramUnit.setText(gram.substring(2, 3));
        }
    }

    private void getBannerData() {
        ActionUtil actionUtil = new ActionUtil(getActivity());
        actionUtil.getBannerData(1);
        actionUtil.setOnPassBackListener(new OnPassBackListener() {

            @Override
            public void passBack(BaseModel model) {
                if (!isActivityAvailable()) {
                    return;
                }
                BannerModel bannerModel = (BannerModel) model;
                if (bannerModel == null) {
                    return;
                }
                if (bannerModel.getErrorMsg() != null) {
                    UIHelper.showToast(getActivity(), bannerModel.getErrorMsg());
                } else {
                    Message msg = new Message();
                    msg.what = STATUS_BANNER_SUCCESS;
                    msg.obj = bannerModel;
                    handle.sendMessage(msg);
                }
            }
        });
    }

    protected void setBannerInfo(List<BannerInfo> bannerList) {
        if (getActivity() == null || getActivity().isFinishing()) {
            return;
        }
        imageList = new ArrayList<>();
        imageList.add(ViewFactory.getImageView(getActivity(), bannerList.get(bannerList.size() - 1).getImageUrl()));

        for (int i = 0; i < bannerList.size(); i++) {
            imageList.add(ViewFactory.getImageView(getActivity(), bannerList.get(i).getImageUrl()));
        }
        // 将第一个ImageView添加进来
        imageList.add(ViewFactory.getImageView(getActivity(), bannerList.get(0).getImageUrl()));

        // 设置循环，在调用setData方法前调用
        cycleViewPager.setCycle(true);

        // 在加载数据前设置是否循环
        cycleViewPager.setData(imageList, bannerList, mAdCycleViewListener);
        // 设置轮播
        cycleViewPager.setWheel(true);

        // 设置轮播时间，默认5000ms
        cycleViewPager.setTime(3000);
        // 设置圆点指示图标组居中显示，默认靠右
        cycleViewPager.setIndicatorCenter();
    }

    private void getHomepageInfo() {
        ActionUtil actionUtil = new ActionUtil(getActivity());
        actionUtil.getHomePageInfo();
        actionUtil.setOnPassBackListener(new OnPassBackListener() {

            @Override
            public void passBack(BaseModel model) {
                if (!isActivityAvailable()) {
                    return;
                }
                HomePageModel hpModel = (HomePageModel) model;
                if (hpModel == null) {

                    UIHelper.showToast(getActivity(), getString(R.string.rank_fail));
                    return;
                }
                if (hpModel.getErrorMsg() != null) {
                    UIHelper.showToast(getActivity(), hpModel.getErrorMsg());
                } else {
                    Message msg = new Message();
                    msg.what = GET_HOMEPAGE_INFO_SUCCESS;
                    msg.obj = hpModel;
                    handle.sendMessage(msg);
                }
            }
        });
    }

    private void initRollNotice() {
        main_notice = (FrameLayout) view.findViewById(R.id.main_notice);
        notice_parent_ll = (LinearLayout) LayoutInflater.from(getActivity()).inflate(R.layout.layout_notice, null);
        notice_vf = ((ViewFlipper) this.notice_parent_ll.findViewById(R.id.homepage_notice_vf));
        ImageView ivDelete = (ImageView) this.notice_parent_ll.findViewById(R.id.iv_delete);
        main_notice.addView(notice_parent_ll);
        ivDelete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                main_notice.setVisibility(View.GONE);
            }
        });

    }

    /**
     * 定时滚动新闻字幕
     */
    private void timedScrollingSubtitles() {
        if (getActivity() == null || getActivity().isFinishing()) {
            return;
        }
        if (task == null) {

            task = new TimerTask() {
                @Override
                public void run() {
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (getActivity() == null || getActivity().isFinishing()) {
                                return;
                            }
                            moveNext();
                        }
                    });

                }
            };
        }
        if (timer == null) {
            timer = new Timer();
        }
        if (timer != null && task != null) {
            timer.schedule(task, 0, 4000);
        }
    }

    private void moveNext() {
        setView(this.mCurrPos, this.mCurrPos + 1);
        this.notice_vf.setInAnimation(getActivity(), R.anim.in_bottomtop);
        this.notice_vf.setOutAnimation(getActivity(), R.anim.out_bottomtop);
        this.notice_vf.showNext();
    }

    private void setView(final int curr, int next) {
        if (getActivity() == null || getActivity().isFinishing()) {
            return;
        }
        View noticeView = LayoutInflater.from(getActivity()).inflate(R.layout.notice_item, null);
        TextView notice_tv = (TextView) noticeView.findViewById(R.id.notice_tv);
        if ((curr < next) && (next > (titleList.size() - 1))) {
            next = 0;
        } else if ((curr > next) && (next < 0)) {
            next = titleList.size() - 1;
        }
        notice_tv.setText(titleList.get(next));
        notice_tv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                ActivityUtils.gotoDynamicDetail(getActivity(), linkUrlArray.get(curr), 0);
            }
        });
        if (notice_vf.getChildCount() > 1) {
            notice_vf.removeViewAt(0);
        }
        notice_vf.addView(noticeView, notice_vf.getChildCount());
        mCurrPos = next;

    }


    private ImageCycleViewListener mAdCycleViewListener = new ImageCycleViewListener() {

        @Override
        public void onImageClick(BannerInfo info, int position, View imageView) {
            if (cycleViewPager.isCycle()) {
                position = position - 1;
                ActivityUtils.gotoDynamicDetail(getActivity(), info.getLinkUrl(), 1);
            }
        }

    };


    private void setRefreshListener() {
        mRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {
                getRecommendProduct("refresh");
                getManageGoldTotalQuantity();
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {

            }
        });
    }

    private void getRecommendProduct(final String action) {
        ActionUtil actionUtil = new ActionUtil(getActivity());
        actionUtil.getRecommendProduct();
        actionUtil.setOnPassBackListener(new OnPassBackListener() {

            @Override
            public void passBack(BaseModel model) {
                if (!isActivityAvailable()) {
                    return;
                }
                RecommendProductModel recommendModel = (RecommendProductModel) model;
                if (getActivity() == null || getActivity().isFinishing()) {
                    return;
                }
                if (recommendModel == null) {
                    if (action.equals("refresh")) {
                        mRefreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
                    }
                    return;
                }
                if (recommendModel.getErrorMsg() != null) {
                    if (action.equals("refresh")) {
                        mRefreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
                    }
                    UIHelper.showToast(getActivity(), recommendModel.getErrorMsg());
                } else {
                    if (action.equals("refresh")) {
                        mRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                    }
                    Message msg = new Message();
                    msg.what = STATUS_RECOMMEND_PRODUCT_SUCCESS;
                    msg.obj = recommendModel;
                    handle.sendMessage(msg);
                }
            }
        });
    }

    private void setProductdata(List<RecommendProductBean> dataList) {
        if (dataList != null) {
            lvRecommendGold.setAdapter(new RecommendGoldAdapter(getActivity(), dataList));
        }
    }


    @Override
    protected void lazyLoad() {
        if (!isPrepared || !isVisible) {
            return;
        }
        requestUnreadMsg();
        getHomepageInfo();
        getGoldPrice();
        getManageGoldTotalQuantity();
        getBannerData();
        getRecommendProduct("");
    }


    @Override
    protected void onInvisible() {
        super.onInvisible();
        if (task != null) {
            task.cancel();
            task = null;
            timer = null;
            isScroll = false;
        }
        if (runnable != null) {
            handle.removeCallbacks(runnable);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onPause() {
        super.onPause();
        if (task != null) {
            task.cancel();
            task = null;
            timer = null;
            isScroll = false;
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (task != null) {
            task.cancel();
            task = null;
            timer = null;
        }
        if (runnable != null) {
            handle.removeCallbacks(runnable);
        }
        ButterKnife.reset(this);
    }

    /*未注册弹出注册对话框*/
    public void showActivityDialog(Context context, String imageUrl, String linkUrl) {
        if (!isActivityDialogShow) {
            new CampaignDialog(context, imageUrl, linkUrl, R.style.MyDialog).show();
            isActivityDialogShow = true;
        }
    }

    @SuppressWarnings("static-access")
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 6 && resultCode == getActivity().RESULT_OK) {
            requestUnreadMsg();
        }
    }


    @Override
    public void onScrollChanged(PullableScrollView scrollView, int x, int y, int oldx, int oldy) {
        // TODO Auto-generated method stub
        if (y <= 0) {   //设置标题的背景颜色
            titleViewMain.setVisibility(View.GONE);
        } else {    //滑动到banner下面设置普通颜色
            titleViewMain.setVisibility(View.VISIBLE);
        }
    }

    @OnClick({R.id.iv_flip, R.id.iv_notification, R.id.invite_layout, R.id.safety_guarantee_layout, R.id.buy_regualar_product_layout, R.id.goldTrendLayout, R.id.media_report_layout, R.id.iv_video_one, R.id.iv_video_two, R.id.btn_policy})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_flip:
                ActivityUtils.gotoNewGuideLines(getActivity());
                break;
            case R.id.iv_notification:
                Intent intent = new Intent();
                intent.setClass(getActivity(), DynamicDetailActivity.class);
                intent.putExtra("activityUrl", UrlUtil.MESSAGE_CENTER);
                intent.putExtra("type", 0);
                startActivityForResult(intent, 6);
                break;
            case R.id.invite_layout:
                if (model != null) {
                    ActivityUtils.gotoDynamicDetail(getActivity(), UrlUtil.INVITE_URL, 0);
                }
                break;
            case R.id.safety_guarantee_layout:
                if (model != null) {
                    ActivityUtils.gotoDynamicDetail(getActivity(), model.getGuaranteePageUrl(), 0);
                }
                break;
            case R.id.buy_regualar_product_layout:
                if (NetworkUtil.isConnected(getActivity())) {
                    ActivityUtils.gotoDynamicDetail(getActivity(), UrlUtil.CURRENT_PRODUCT_DETAIL, 11);
                } else {
                    UIHelper.showToast(getActivity(), getString(R.string.rank_fail));
                }
                break;
            case R.id.goldTrendLayout:
                ActivityUtils.gotoGPriceTrend(getActivity(), UrlUtil.GOLD_TREND);
                break;
            case R.id.btn_policy:
                ActivityUtils.gotoDynamicDetail(getActivity(), UrlUtil.INSURANCE_PAGE_URL, 5);
                break;
            case R.id.media_report_layout:
                if (getActivity() != null && getActivity() instanceof MainActivity) {
                    ((MainActivity) getActivity()).skipToDisCoverey();
                }
                break;
            case R.id.iv_video_one:
                if (mediaList != null) {
                    openMediaPage(mediaList.get(0).getLinkUrl());
                }
                break;
            case R.id.iv_video_two:
                if (mediaList != null) {
                    openMediaPage(mediaList.get(1).getLinkUrl());
                }
                break;
        }
    }

    private void openMediaPage(String linkUrl) {
        Intent mediaIntent = new Intent(getActivity(), DynamicDetailActivity.class);
        mediaIntent.putExtra("activityUrl", linkUrl);
        mediaIntent.putExtra("type", 12);
        startActivity(mediaIntent);
    }
}
